{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于矩阵的协同过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# coding: utf-8\n",
    "# -*- coding:utf-8 -*-\n",
    "import sys\n",
    "reload(sys)\n",
    "sys.setdefaultencoding(\"utf-8\")\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import cPickle\n",
    "import scipy.io as sio\n",
    "\n",
    "#距离\n",
    "import scipy.spatial.distance as ssd\n",
    "\n",
    "from numpy.random import random  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 从文件读入输入，初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 读入数据做初始化\n",
    "    \n",
    "#用户和item的索引\n",
    "user_index = cPickle.load(open(\"user_index.pkl\", 'rb'))\n",
    "item_index = cPickle.load(open(\"item_index.pkl\", 'rb'))\n",
    "\n",
    "n_users = len(user_index)\n",
    "n_items = len(item_index)\n",
    "    \n",
    "#用户-物品关系矩阵R\n",
    "#user_item_scores = sio.mmread(\"user_items_scores\").todense()\n",
    "    \n",
    "#倒排表\n",
    "##每个用户播放的歌曲\n",
    "user_items = cPickle.load(open(\"user_items.pkl\", 'rb'))\n",
    "##事件参加的用户\n",
    "item_users = cPickle.load(open(\"item_users.pkl\", 'rb'))\n",
    "\n",
    "#所有item之间的相似度\n",
    "#similarity_matrix = cPickle.load(open(\"items_similarity.pkl\", 'rb'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 训练数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(211690, 9)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('triplet_dataset_sub_song_merged.csv')\n",
    "\n",
    "#每个用户的总播放次数\n",
    "triplet_train_sum_df = train[['user','listen_count']].groupby('user').sum().reset_index()\n",
    "triplet_train_sum_df.rename(columns={'listen_count':'total_listen_count'},inplace=True)\n",
    "\n",
    "#每首歌曲的播放比例\n",
    "train = pd.merge(train,triplet_train_sum_df)\n",
    "train['fractional_play_count'] = train['listen_count']/train['total_listen_count']\n",
    "del triplet_train_sum_df\n",
    "\n",
    "song_count_df = pd.read_csv('song_playcount_df.csv')\n",
    "song_count_subset = song_count_df.head(n=1000)\n",
    "song_subset = list(song_count_subset.song)\n",
    "trian_sub = train[train.song.isin(song_subset)]\n",
    "\n",
    "del train\n",
    "\n",
    "trian_sub.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "      <th>total_listen_count</th>\n",
       "      <th>fractional_play_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "      <td>861</td>\n",
       "      <td>0.001161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAJJDS12A8C13A3FB</td>\n",
       "      <td>1</td>\n",
       "      <td>I Got Mine</td>\n",
       "      <td>Attack &amp; Release</td>\n",
       "      <td>The Black Keys</td>\n",
       "      <td>2008</td>\n",
       "      <td>861</td>\n",
       "      <td>0.001161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAMPRJ12A8AE45F38</td>\n",
       "      <td>20</td>\n",
       "      <td>Rorol</td>\n",
       "      <td>Identification Parade</td>\n",
       "      <td>Octopus Project</td>\n",
       "      <td>2002</td>\n",
       "      <td>861</td>\n",
       "      <td>0.023229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOCAFDI12A8C13D10E</td>\n",
       "      <td>4</td>\n",
       "      <td>TTHHEE PPAARRTTYY</td>\n",
       "      <td>Justice</td>\n",
       "      <td>Justice</td>\n",
       "      <td>0</td>\n",
       "      <td>861</td>\n",
       "      <td>0.004646</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOCJCVE12A8C13CDDB</td>\n",
       "      <td>11</td>\n",
       "      <td>One Minute To Midnight</td>\n",
       "      <td>Justice</td>\n",
       "      <td>Justice</td>\n",
       "      <td>0</td>\n",
       "      <td>861</td>\n",
       "      <td>0.012776</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        user                song  \\\n",
       "0   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAFTRR12AF72A8D4D   \n",
       "2   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAJJDS12A8C13A3FB   \n",
       "4   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAMPRJ12A8AE45F38   \n",
       "15  5a905f000fc1ff3df7ca807d57edb608863db05d  SOCAFDI12A8C13D10E   \n",
       "18  5a905f000fc1ff3df7ca807d57edb608863db05d  SOCJCVE12A8C13CDDB   \n",
       "\n",
       "    listen_count                          title                release  \\\n",
       "0              1  Harder Better Faster Stronger              Discovery   \n",
       "2              1                     I Got Mine       Attack & Release   \n",
       "4             20                          Rorol  Identification Parade   \n",
       "15             4              TTHHEE PPAARRTTYY                Justice   \n",
       "18            11         One Minute To Midnight                Justice   \n",
       "\n",
       "        artist_name  year  total_listen_count  fractional_play_count  \n",
       "0         Daft Punk  2007                 861               0.001161  \n",
       "2    The Black Keys  2008                 861               0.001161  \n",
       "4   Octopus Project  2002                 861               0.023229  \n",
       "15          Justice     0                 861               0.004646  \n",
       "18          Justice     0                 861               0.012776  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trian_sub.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化模型参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "K = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据训练数据训练模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始化参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#init parameters\n",
    "#bias\n",
    "bi = np.zeros(n_items)  \n",
    "bu = np.zeros(n_users)  \n",
    "    \n",
    "#the small matrix\n",
    "P = random((n_users,K))/10*(np.sqrt(K))\n",
    "Q = random((K, n_items))/10*(np.sqrt(K))  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读入训练数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n_samples = trian_sub.shape[0]\n",
    "\n",
    "mu = 0.0\n",
    "uids = []  #每条记录的用户索引\n",
    "i_ids = [] #每条记录的item索引\n",
    "\n",
    "#用户-Item关系矩阵R，临时变量，训练完了R不再需要\n",
    "R= np.matrix(np.zeros(shape=(n_users, n_items)), float)\n",
    "    \n",
    "for i in range(n_samples):  \n",
    "    uid = user_index[trian_sub.iloc[i]['user']]  #用户\n",
    "    iid = item_index[trian_sub.iloc[i]['song']] #歌曲\n",
    "        \n",
    "    uids.append(uid)\n",
    "    i_ids.append(iid)\n",
    "        \n",
    "    R[uid,iid] = trian_sub.iloc[i]['fractional_play_count'] #interested\n",
    "    mu += R[uid,iid]\n",
    "\n",
    "mu /= n_samples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 迭代，随机梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#根据当前参数，预测用户uid对Item（i_id）的打分     \n",
    "def pred_SVD(uid, i_id):\n",
    "    score = mu + bi[i_id] + bu[uid] + np.dot(P[uid,:], Q[:,i_id])  \n",
    "    return score    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n_steps = 50\n",
    "gamma=0.04\n",
    "Lambda=0.15\n",
    "\n",
    "for step in range(n_steps):  \n",
    "    print 'the ',step,'-th  step is running'  \n",
    "    \n",
    "    rmse_sum=0.0 \n",
    "            \n",
    "    #将训练样本打散顺序\n",
    "    kk = np.random.permutation(n_samples)  \n",
    "    for j in range(n_samples):  \n",
    "        #每次一个训练样本\n",
    "        index = kk[j]  \n",
    " \n",
    "        uid = uids[index]\n",
    "        iid = i_ids[index]\n",
    " \n",
    "        #预测残差\n",
    "        eui = R[uid,iid] - pred_SVD(uid,iid)\n",
    "        \n",
    "        #残差平方和\n",
    "        rmse_sum += eui**2\n",
    "               \n",
    "        #随机梯度下降，更新\n",
    "        bu[uid]+= gamma*(eui - Lambda*bu[uid])  \n",
    "        bi[iid]+= gamma*(eui - Lambda*bi[iid]) \n",
    "            \n",
    "        for k in range(K):\n",
    "            #P,Q 同时更新，temp暂存P的更新之的值\n",
    "            temp = P[uid,k] + gamma * eui * Q[k,iid] - Lambda * P[uid,k]\n",
    "               \n",
    "            Q[k,iid] += gamma * eui * P[uid,k] - Lambda * Q[k,iid]\n",
    "            P[uid,k] = temp\n",
    "                \n",
    "    #学习率递减\n",
    "    gamma=gamma*0.93  \n",
    "    print(\"the rmse of the {} th step on train data is:{}\".format(step, rmse_sum))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算用户对每个item的预测打分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cur_user = '5a905f000fc1ff3df7ca807d57edb608863db05d'\n",
    "\n",
    "cur_user_id = user_index[cur_user]\n",
    "cur_user_items = user_items[cur_user_id]\n",
    "\n",
    "sim_accumulate=0.0  \n",
    "rat_acc=0.0 \n",
    "\n",
    "user_items_scores = np.zeros(n_items)\n",
    "\n",
    "for i in range(n_items):  # all items of user not played\n",
    "    if i not in cur_user_items:\n",
    "        user_items_scores[i] = pred_SVD(cur_user_id, i)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  2.21764712e-02,   1.47285982e-02,   1.43300229e-02,\n",
       "         7.04030393e-03,   1.19198367e-02,   8.24267452e-03,\n",
       "         7.48725302e-03,   1.40529062e-02,   1.31342551e-02,\n",
       "         1.35256057e-02,   1.17351548e-02,   1.06003388e-02,\n",
       "         9.85538957e-03,   5.96368046e-03,   4.85065947e-03,\n",
       "         0.00000000e+00,   5.31942139e-03,   6.83044451e-03,\n",
       "         1.23947960e-02,   3.38415321e-03,   5.27966451e-03,\n",
       "         1.09986976e-02,   5.34582543e-03,   1.04081062e-02,\n",
       "         9.10349306e-03,   9.66908466e-03,   0.00000000e+00,\n",
       "         6.45395799e-03,   4.35505055e-03,   5.21696863e-03,\n",
       "         6.84828079e-03,   8.71633925e-03,   0.00000000e+00,\n",
       "         1.03470714e-02,   1.28327912e-02,   4.24522581e-03,\n",
       "         0.00000000e+00,   4.66904419e-03,   5.00378429e-03,\n",
       "         0.00000000e+00,   8.74415160e-03,   3.89233767e-03,\n",
       "         4.73678728e-03,   1.30321921e-02,   1.08498208e-02,\n",
       "         1.04873735e-02,   9.11081925e-03,   4.41758810e-03,\n",
       "         7.44880968e-03,   1.12407186e-02,   7.36295376e-03,\n",
       "         4.22263606e-03,   3.53875870e-03,   4.56534930e-03,\n",
       "         9.12410162e-03,   6.20924629e-03,   9.18540429e-03,\n",
       "         4.71626663e-03,   5.20718591e-03,   1.12605986e-02,\n",
       "         4.52358345e-03,   7.97675085e-03,   9.90833977e-03,\n",
       "         4.26601391e-03,   4.18958453e-03,   4.87714415e-03,\n",
       "         4.92517015e-03,   8.70162090e-03,   0.00000000e+00,\n",
       "         4.33070478e-03,   4.99457901e-03,   3.96563152e-03,\n",
       "         5.02395830e-03,   0.00000000e+00,   3.97764151e-03,\n",
       "         5.36383474e-03,   4.83430198e-03,   4.72502963e-03,\n",
       "         6.32168876e-03,   4.39329085e-03,   7.98391064e-03,\n",
       "         4.61408381e-03,   3.99959267e-03,   6.05308369e-03,\n",
       "         4.01578466e-03,   0.00000000e+00,   4.04714390e-03,\n",
       "         5.26776985e-03,   4.41849435e-03,   3.83512012e-03,\n",
       "         1.20846314e-02,   4.07999917e-03,   4.24081545e-03,\n",
       "         9.61657212e-03,   2.08901980e-02,   5.90385355e-03,\n",
       "         3.35245534e-03,   4.93375286e-03,   4.96669910e-03,\n",
       "         4.37667122e-03,   4.27496563e-03,   1.90947116e-02,\n",
       "         3.70313679e-03,   3.85556892e-03,   0.00000000e+00,\n",
       "         4.66697661e-03,   3.46542221e-03,   2.89638488e-03,\n",
       "         0.00000000e+00,   1.19261963e-02,   4.10046775e-03,\n",
       "         3.91070289e-03,   1.22034708e-02,   4.18847689e-03,\n",
       "         6.22983472e-03,   3.75300029e-03,   4.34724143e-03,\n",
       "         5.69140892e-02,   1.57807536e-02,   6.70544590e-03,\n",
       "         3.88068235e-03,   9.81298498e-03,   1.16252050e-02,\n",
       "         2.83896563e-03,   4.89210577e-03,   4.69759192e-03,\n",
       "         5.29919448e-03,   6.43389887e-03,   5.94293532e-03,\n",
       "         3.78883775e-03,   1.46387025e-03,   1.14793602e-02,\n",
       "         5.88025645e-03,   4.08185857e-03,   1.71492346e-03,\n",
       "         1.86167479e-02,   1.35120921e-02,   3.90944125e-03,\n",
       "         7.54056437e-03,   5.62052713e-03,   3.68377262e-03,\n",
       "         6.11546994e-03,   3.65763104e-03,   3.84825980e-03,\n",
       "         3.57710132e-03,   4.29873895e-03,   8.29900431e-03,\n",
       "         5.20636206e-03,   9.33178526e-03,   4.33892069e-03,\n",
       "         1.03006577e-02,   9.51778053e-03,   1.17115884e-02,\n",
       "         3.97413993e-03,   5.98119253e-03,   1.44201019e-02,\n",
       "         9.07420461e-03,   5.42722280e-03,   4.91135343e-03,\n",
       "         3.94627286e-03,   4.77971539e-03,   4.29916923e-03,\n",
       "         4.89165011e-03,   3.34737728e-03,   6.29333059e-03,\n",
       "         3.64401372e-03,   3.66829742e-03,   4.07639029e-03,\n",
       "         1.51843367e-02,   1.42205760e-02,   4.24276827e-03,\n",
       "         1.36371241e-02,   4.78706309e-03,   4.94164953e-03,\n",
       "         0.00000000e+00,   1.12008417e-02,   3.44300333e-03,\n",
       "         4.00361353e-03,   1.43453323e-02,   4.12961120e-03,\n",
       "         4.23989886e-03,   4.53436596e-03,   7.23216087e-03,\n",
       "         9.98846243e-03,   5.40618915e-03,   3.15532911e-03,\n",
       "         5.46324789e-03,   5.02342461e-03,   1.08274928e-02,\n",
       "         0.00000000e+00,   1.70013053e-02,   0.00000000e+00,\n",
       "         3.05749914e-03,   8.81028668e-03,   5.03060465e-03,\n",
       "         1.57043435e-02,   2.42001846e-03,   3.31139614e-03,\n",
       "         3.57144849e-03,   8.53236694e-03,   6.95210158e-03,\n",
       "         0.00000000e+00,   5.76964759e-03,   3.93199192e-03,\n",
       "         4.42785357e-03,   5.10758094e-03,   4.45639502e-03,\n",
       "         3.06017581e-03,   4.62718244e-03,   1.19015003e-02,\n",
       "         5.77065594e-03,   2.05307402e-02,   1.21072386e-02,\n",
       "         1.99515495e-02,   2.87424268e-02,   1.04230958e-02,\n",
       "         1.44492335e-02,   5.37753216e-03,   9.37457240e-03,\n",
       "         4.83383512e-03,   1.12192469e-02,   1.24240797e-02,\n",
       "         3.68029837e-03,   3.63994974e-03,   4.46401807e-03,\n",
       "         5.64213892e-03,   9.91784269e-03,   5.14891075e-03,\n",
       "         9.48989889e-03,   5.99178623e-03,   3.63603747e-03,\n",
       "         1.01057473e-02,   4.55782099e-03,   3.09573143e-03,\n",
       "         1.26019811e-02,   1.46474855e-02,   1.26228596e-02,\n",
       "         0.00000000e+00,   1.09542425e-02,   7.90940775e-03,\n",
       "         1.70738232e-02,   3.64190033e-03,   9.94355513e-03,\n",
       "         4.45956455e-03,   1.23369821e-02,   9.22714260e-03,\n",
       "         7.20495269e-03,   3.42800482e-03,   3.48680081e-03,\n",
       "         5.14740361e-03,   6.00569896e-03,   3.70597218e-03,\n",
       "         1.22479891e-02,   5.75391069e-03,   4.92368941e-03,\n",
       "         0.00000000e+00,   2.68333957e-03,   3.82705201e-03,\n",
       "         1.40780711e-02,   5.35446437e-03,   1.20374927e-02,\n",
       "         4.46870485e-03,   8.12967061e-03,   4.82831054e-03,\n",
       "         7.53585621e-03,   6.10202865e-03,   1.30662774e-02,\n",
       "         5.90319806e-03,   7.19665278e-03,   2.17119240e-02,\n",
       "         1.94023071e-02,   5.31752533e-03,   1.71011551e-02,\n",
       "         7.48949486e-03,   1.15187017e-02,   7.44910265e-03,\n",
       "         2.46938562e-03,   4.84592140e-03,   2.51458397e-03,\n",
       "         4.82647216e-03,   7.37574902e-03,   1.00140175e-02,\n",
       "         5.86530348e-02,   2.24213765e-03,   9.53816231e-03,\n",
       "         7.22005437e-03,   7.47125355e-03,   1.48212250e-02,\n",
       "         4.30070234e-03,   4.29685460e-03,   3.26780484e-03,\n",
       "         1.06147816e-02,   1.01167266e-02,   3.78319297e-03,\n",
       "         5.29030389e-03,   2.55192468e-02,   9.46376960e-03,\n",
       "         3.73228429e-03,   7.59106589e-03,   1.42000465e-02,\n",
       "         3.08284064e-03,   1.49224888e-02,   3.52478858e-03,\n",
       "         4.51389925e-03,   1.23137889e-02,   5.51089596e-03,\n",
       "         1.05858097e-02,   5.22245272e-03,   2.12235490e-03,\n",
       "         1.17106375e-02,   3.33678984e-03,   6.46308902e-03,\n",
       "         2.62037490e-02,   3.04018274e-02,   4.80490818e-03,\n",
       "         9.20902472e-03,   5.96385504e-03,   5.49238355e-03,\n",
       "         8.25726129e-03,   4.21297603e-03,   8.39965157e-03,\n",
       "         6.40664549e-03,   0.00000000e+00,   1.04198342e-02,\n",
       "         1.00465870e-02,   9.67526798e-03,   6.14466901e-03,\n",
       "         6.69853896e-03,   6.31617790e-03,   3.83597991e-03,\n",
       "         5.80632461e-03,   5.79106894e-03,   4.18568068e-03,\n",
       "         4.19077754e-03,   3.34080048e-02,   0.00000000e+00,\n",
       "         5.81744717e-03,   5.52877450e-03,   8.62430340e-03,\n",
       "         3.38665088e-03,   4.93540816e-03,   3.90772322e-03,\n",
       "         9.60373780e-03,   3.24001081e-02,   5.04361415e-03,\n",
       "         4.54335731e-03,   3.62480739e-03,   5.21816584e-03,\n",
       "         9.40514961e-03,   1.33476341e-02,   6.86725286e-03,\n",
       "         5.65747248e-03,   1.20580917e-02,   1.10726862e-02,\n",
       "         5.05498313e-03,   0.00000000e+00,   1.13153651e-02,\n",
       "         9.69671270e-03,   1.05343231e-02,   3.65505361e-03,\n",
       "         6.09977769e-03,   7.02478989e-03,   5.70537727e-03,\n",
       "         3.41612981e-03,   5.38282295e-03,   7.70843268e-03,\n",
       "         1.54334451e-02,   3.93708637e-03,   7.78370617e-03,\n",
       "         3.92565516e-03,   4.11441940e-03,   1.48457560e-02,\n",
       "         1.07435311e-02,   6.43623837e-03,   4.90798908e-03,\n",
       "         0.00000000e+00,   6.37448954e-03,   9.45658641e-03,\n",
       "         2.91147287e-02,   3.73782926e-03,   1.38490064e-02,\n",
       "         9.31924247e-03,   4.09405951e-03,   1.46009298e-02,\n",
       "         4.69586407e-03,   5.75884586e-03,   4.55222621e-03,\n",
       "         5.57466191e-03,   1.26693079e-02,   4.29560821e-03,\n",
       "         5.33675812e-03,   6.84839544e-03,   1.10836962e-02,\n",
       "         7.24605918e-03,   4.45983241e-03,   7.22196761e-03,\n",
       "         3.91814697e-03,   7.35647456e-03,   1.44245548e-02,\n",
       "         1.96143190e-02,   0.00000000e+00,   4.46150532e-03,\n",
       "         4.22839733e-03,   3.36700331e-03,   1.04329509e-02,\n",
       "         5.21398128e-03,   6.83174462e-03,   0.00000000e+00,\n",
       "         4.01601889e-03,   5.44833214e-03,   6.94258534e-03,\n",
       "         5.62600149e-03,   1.82653575e-03,   3.40037161e-03,\n",
       "         4.86814803e-03,   6.25549819e-03,   2.06892728e-02,\n",
       "         5.35681272e-03,   8.25827763e-03,   2.59696682e-04,\n",
       "         5.01789071e-03,   7.28333322e-03,   5.44922071e-03,\n",
       "         0.00000000e+00,   9.15384393e-03,   6.05839138e-03,\n",
       "         5.38761729e-03,   8.80125863e-03,   8.56370949e-03,\n",
       "         1.03326919e-02,   3.06897482e-03,   3.38076549e-03,\n",
       "         4.39833976e-03,   1.10646571e-02,   1.30506742e-02,\n",
       "         6.88998676e-03,   4.62368186e-03,   1.36706630e-02,\n",
       "         5.32718435e-03,   6.26372471e-03,   9.63067324e-03,\n",
       "         1.20663366e-02,   5.68116153e-03,   3.64903331e-03,\n",
       "         5.31890544e-03,   8.37095264e-03,   3.61914785e-03,\n",
       "         4.97076467e-03,   2.68402161e-03,   3.87644135e-03,\n",
       "         6.62404433e-03,   0.00000000e+00,   2.55877786e-03,\n",
       "         6.59887618e-03,   2.69039086e-03,   1.33468469e-02,\n",
       "         5.04107602e-03,   7.18986841e-03,   7.51486214e-03,\n",
       "         4.08224010e-03,   1.92296742e-02,   5.95316757e-03,\n",
       "         1.10952414e-02,   2.03648393e-02,   9.72740804e-03,\n",
       "         4.24677617e-03,   9.01053820e-03,   6.92179271e-03,\n",
       "         5.84558184e-03,   6.90813649e-03,   2.60144562e-03,\n",
       "         3.85506529e-03,   5.04147962e-03,   0.00000000e+00,\n",
       "         5.01053169e-03,   4.50039687e-03,   0.00000000e+00,\n",
       "         6.73800036e-02,   5.24552807e-03,   4.00922329e-03,\n",
       "         5.19816236e-03,   4.80294128e-03,   6.43217694e-03,\n",
       "         6.65966551e-03,   4.81807590e-03,   5.98228325e-03,\n",
       "         1.89174470e-02,   7.56509240e-03,   4.05809037e-03,\n",
       "         7.41198978e-03,   4.39561979e-03,   3.66895188e-03,\n",
       "         8.67965725e-03,   4.71567227e-03,   6.06085843e-03,\n",
       "         6.82633483e-03,   6.73664488e-03,   1.00990903e-02,\n",
       "         1.61135745e-02,   3.74059490e-02,   3.35063437e-03,\n",
       "         1.52421505e-02,   1.04563297e-02,   5.51406436e-03,\n",
       "         5.05106338e-03,   6.95071649e-03,   4.44891364e-03,\n",
       "         7.80448553e-03,   5.37021575e-03,   2.49663082e-03,\n",
       "         4.27777450e-03,   1.21270967e-02,   1.19377168e-02,\n",
       "         2.29839359e-03,   6.98922235e-03,   5.54990232e-03,\n",
       "         0.00000000e+00,   3.31532289e-03,   1.16868150e-02,\n",
       "         0.00000000e+00,   2.57491889e-03,   0.00000000e+00,\n",
       "         4.92163639e-03,   0.00000000e+00,   4.38907029e-03,\n",
       "         1.15120164e-02,   0.00000000e+00,   4.84991550e-02,\n",
       "         3.03729952e-03,   5.33365797e-03,   5.78661568e-03,\n",
       "        -1.82476813e-04,   8.40339814e-03,   4.09928761e-03,\n",
       "         1.52230845e-02,   6.56913976e-03,   1.03559264e-02,\n",
       "         8.07164130e-03,   7.84622621e-03,   7.60676145e-03,\n",
       "         9.59659757e-04,   1.21267609e-02,   6.94284605e-03,\n",
       "         4.34736781e-03,   2.04925114e-02,   5.22347437e-03,\n",
       "         4.53520194e-03,   0.00000000e+00,   6.80154988e-03,\n",
       "         1.97936066e-02,   2.47380577e-02,   5.06157381e-03,\n",
       "         1.72483285e-02,   8.40873413e-03,   1.05568197e-02,\n",
       "         6.61775395e-03,   3.51406615e-03,   1.07430877e-02,\n",
       "         3.71022208e-03,   3.17607239e-03,   9.58228760e-03,\n",
       "         9.69516954e-03,   6.43279487e-03,   1.34366095e-02,\n",
       "         5.41356541e-03,   4.54681207e-03,   0.00000000e+00,\n",
       "         6.12350487e-03,   4.98424304e-03,   3.58342671e-03,\n",
       "         3.61206199e-03,   1.13249349e-02,   2.91248409e-03,\n",
       "         1.10776283e-02,   6.25287515e-03,   4.32557301e-03,\n",
       "         0.00000000e+00,   4.69613538e-03,   1.37066574e-02,\n",
       "         8.73548229e-03,   0.00000000e+00,   5.19928889e-03,\n",
       "         7.88134389e-03,   5.59617455e-03,   7.99533114e-03,\n",
       "         6.93980725e-03,   4.64740823e-03,   4.95336479e-03,\n",
       "         0.00000000e+00,   3.89893745e-03,   3.45678809e-03,\n",
       "         6.31859117e-03,   1.13767101e-02,   2.97686517e-02,\n",
       "         4.34611146e-03,   5.04928023e-03,   4.57074561e-03,\n",
       "         4.36365755e-03,   1.96465686e-02,   6.42223840e-03,\n",
       "         2.87831349e-03,   1.36535970e-02,   3.64136340e-03,\n",
       "         5.92829064e-03,   5.39878699e-03,   4.61933669e-03,\n",
       "         8.95660692e-03,   0.00000000e+00,   1.02403280e-02,\n",
       "         3.57394536e-03,   1.06133592e-02,   8.12647404e-03,\n",
       "         6.92141657e-04,   8.93181149e-03,   4.44437553e-03,\n",
       "         1.05377393e-02,   1.68641252e-02,   1.23673977e-02,\n",
       "         1.01437776e-02,   6.40770892e-03,   2.54816453e-02,\n",
       "         9.50048277e-03,   3.39157327e-03,   6.49050884e-03,\n",
       "         9.57207884e-03,   7.79071937e-03,   1.12130457e-02,\n",
       "         1.02229950e-02,   4.62994627e-03,   2.81351040e-02,\n",
       "         2.54256783e-02,   6.18051659e-03,   3.35038062e-03,\n",
       "         4.69698795e-03,   3.72485281e-03,   5.02162938e-03,\n",
       "         5.25405762e-03,   6.89559558e-03,   2.53279548e-02,\n",
       "         9.47706776e-03,   1.24373663e-02,   5.93630729e-03,\n",
       "         7.39279979e-03,   3.53910524e-03,   2.11863920e-02,\n",
       "         7.42553265e-03,   7.20666039e-03,   1.61287823e-02,\n",
       "         4.50052465e-03,   4.86492421e-03,   5.46697596e-03,\n",
       "         2.88285406e-03,   1.45262995e-02,   8.46631751e-03,\n",
       "         5.77778844e-03,   8.60633846e-03,   3.58686171e-03,\n",
       "         5.07274892e-02,   9.81013726e-03,   9.24039871e-03,\n",
       "         4.15268919e-03,   3.47215018e-03,   2.96078938e-03,\n",
       "         1.76792114e-03,   4.99148859e-03,   4.07975752e-03,\n",
       "         4.72231264e-03,   1.28653318e-02,   4.64245827e-03,\n",
       "         7.31343819e-03,   1.25490078e-02,   5.48329337e-03,\n",
       "         3.85587169e-03,   4.45995940e-03,   8.63484456e-03,\n",
       "         3.51180073e-03,   4.35786216e-03,   1.09063319e-02,\n",
       "         0.00000000e+00,   7.77307961e-03,   3.30236764e-03,\n",
       "         2.93883314e-03,   4.68941645e-03,   1.25395114e-02,\n",
       "         4.18301990e-03,   5.45566966e-03,   5.01539662e-03,\n",
       "         2.44726483e-03,   0.00000000e+00,   6.56210887e-03,\n",
       "         8.80244477e-03,   8.71697786e-03,   2.28019888e-02,\n",
       "         8.27674075e-03,   3.32131245e-03,   6.18563465e-03,\n",
       "         3.27281271e-03,   7.94391596e-03,   0.00000000e+00,\n",
       "         9.24717049e-03,   2.33311921e-03,   3.05487339e-03,\n",
       "         4.52683850e-03,   4.09022975e-03,   9.75704645e-03,\n",
       "         4.74446959e-03,   1.15855873e-02,   3.39043171e-03,\n",
       "         7.72625265e-03,   4.93585961e-03,   5.55501852e-03,\n",
       "         4.81842184e-03,   5.86476268e-03,   6.19186497e-03,\n",
       "         4.40245408e-03,   3.68507704e-03,   9.21898728e-03,\n",
       "         2.00853275e-02,   1.24324948e-02,   5.39662643e-03,\n",
       "         4.72162942e-03,   5.88795042e-03,   1.07253479e-02,\n",
       "         6.76330200e-03,   1.13394706e-02,   4.11620329e-03,\n",
       "         2.52386852e-02,   0.00000000e+00,   8.38201107e-03,\n",
       "         1.06698861e-02,   6.85171511e-03,   6.23656821e-03,\n",
       "         6.45294581e-03,   4.02828165e-03,   2.68967952e-03,\n",
       "         8.45533627e-03,   7.23079098e-03,   2.91302009e-03,\n",
       "         3.22112159e-03,   1.63530152e-02,   6.92415794e-03,\n",
       "         5.29999283e-03,   4.36291666e-03,   5.23933633e-03,\n",
       "         5.26238728e-03,   2.09974847e-03,   5.19549919e-03,\n",
       "         0.00000000e+00,   0.00000000e+00,   4.62268924e-03,\n",
       "         7.13015147e-03,   4.72681066e-03,   4.22188701e-03,\n",
       "         1.13304106e-02,   1.10750920e-02,   3.55519774e-03,\n",
       "         5.72247925e-03,   4.87818238e-03,   3.19745838e-03,\n",
       "         3.74761982e-03,   3.03697656e-03,   1.53634614e-02,\n",
       "         1.21127173e-02,   0.00000000e+00,   8.44917787e-03,\n",
       "         8.45055544e-03,   8.22104372e-03,   6.19421061e-03,\n",
       "         3.65998071e-03,   5.45986318e-03,   3.99487958e-03,\n",
       "         6.93053557e-03,   5.96971991e-03,   1.21021274e-02,\n",
       "         7.01934994e-03,   9.23773029e-04,   5.52154593e-03,\n",
       "         4.55594732e-03,   4.23986020e-03,   3.28281557e-03,\n",
       "         5.20265378e-03,   3.59932911e-03,   1.05177984e-02,\n",
       "         3.87628323e-03,   1.07339050e-02,   1.29059739e-02,\n",
       "         9.08476985e-03,   7.47335479e-03,   1.33210600e-02,\n",
       "         7.07415255e-03,   4.68796631e-03,   3.30084747e-03,\n",
       "         5.41043918e-03,   3.18370493e-03,   8.28600383e-03,\n",
       "         1.33302075e-02,   7.18467039e-03,   3.94051510e-03,\n",
       "         8.94254175e-03,   3.13461121e-02,   5.14854481e-03,\n",
       "         7.48845931e-03,   4.95449637e-03,   3.01502236e-03,\n",
       "         6.49048236e-03,   2.87879141e-03,   7.25571229e-03,\n",
       "         7.78606839e-03,   9.76989865e-03,   3.58657579e-03,\n",
       "         5.07689201e-03,   8.08713497e-03,   2.20922446e-02,\n",
       "         6.06185000e-03,   1.13274042e-02,   1.99461274e-02,\n",
       "         4.45169391e-03,   3.90486970e-03,   0.00000000e+00,\n",
       "         5.35789581e-03,   7.19772871e-03,   1.29568767e-02,\n",
       "         3.41985623e-03,   7.42299415e-03,   2.14271847e-02,\n",
       "         6.68720869e-03,   0.00000000e+00,   2.24597938e-02,\n",
       "         2.65920692e-03,   5.85167453e-03,   8.88337159e-03,\n",
       "         6.57070007e-03,   4.02608083e-03,   9.88201915e-03,\n",
       "         0.00000000e+00,   8.37885257e-03,   1.02832579e-02,\n",
       "         1.24561641e-02,   3.13338861e-03,   1.22403499e-02,\n",
       "         5.99058329e-03,   9.75782129e-03,   7.27924021e-03,\n",
       "         1.67450636e-03,   9.79741095e-03,   8.23079375e-03,\n",
       "         7.25948532e-03,   6.71752024e-03,   4.65932871e-03,\n",
       "         4.65517093e-03,   0.00000000e+00,   6.22824149e-03,\n",
       "         4.25725243e-02,   3.08897660e-03,   4.37828689e-03,\n",
       "         1.45876929e-02,   0.00000000e+00,   6.20373680e-03,\n",
       "         4.71037113e-03,   6.07755133e-03,   3.12538753e-03,\n",
       "         3.41135620e-03,   8.97727338e-03,   2.50108538e-02,\n",
       "         6.18344220e-03,   3.15140458e-03,   7.27758917e-03,\n",
       "         9.40073249e-04,   6.93056007e-03,   2.55303716e-03,\n",
       "         4.89618170e-03,   9.67990010e-03,   5.37698822e-03,\n",
       "         5.82312047e-03,   8.02127013e-03,   4.15299071e-03,\n",
       "         3.88185256e-03,   0.00000000e+00,   3.08974726e-03,\n",
       "         3.86551918e-03,   4.92196202e-03,   3.43533480e-03,\n",
       "         8.80044120e-03,   5.26699187e-03,   2.54286368e-03,\n",
       "         1.13564881e-02,   2.97804923e-03,   5.36139376e-03,\n",
       "         1.47443414e-02,   1.51371084e-02,   4.34600323e-03,\n",
       "         4.56794681e-03,   1.05457338e-02,   7.59638261e-03,\n",
       "         0.00000000e+00,   6.84353950e-03,   6.61863825e-03,\n",
       "         1.67873889e-03,   1.22107202e-02,   5.06837476e-03,\n",
       "         8.13531798e-03,   5.68514089e-03,   4.33698589e-03,\n",
       "         3.14936975e-03,   8.93642514e-03,   5.16135848e-03,\n",
       "         1.31223682e-02,   4.32160550e-03,   2.44694591e-02,\n",
       "         1.46978605e-02,   2.05694866e-02,   3.73662157e-03,\n",
       "         5.24474236e-03,   0.00000000e+00,   3.39610592e-03,\n",
       "         5.02493325e-03,   6.86555395e-03,   0.00000000e+00,\n",
       "         1.73687527e-03,   5.95113113e-03,   1.06322195e-02,\n",
       "         0.00000000e+00,   0.00000000e+00,   5.33538328e-03,\n",
       "         1.03512323e-02,   9.60671344e-03,   4.51621848e-03,\n",
       "         8.15027353e-03,   1.50682350e-02,   1.53818259e-02,\n",
       "         3.28630350e-03,   5.62786954e-03,   9.79613127e-03,\n",
       "         7.61943366e-03,   2.88576600e-02,   3.40619539e-03,\n",
       "         1.21880760e-02,   6.04555369e-03,   7.41630242e-03,\n",
       "         7.80826513e-03,   4.27354376e-03,   7.71898129e-05,\n",
       "         4.65891459e-03,   4.84319533e-03,   1.25215113e-02,\n",
       "         3.60742937e-03,   5.81132562e-03,   3.52051455e-03,\n",
       "         7.00307611e-03,   3.97962411e-03,   1.18386978e-02,\n",
       "         2.35097472e-02,   7.88023710e-03,   5.52025443e-03,\n",
       "         0.00000000e+00,   8.46230007e-03,   2.93490374e-03,\n",
       "         1.19418744e-02,   1.75140871e-02,   9.93028663e-03,\n",
       "         4.61181406e-03,   5.79310978e-03,   3.10807783e-03,\n",
       "         7.04692487e-03,   4.70276555e-03,   3.88834642e-03,\n",
       "         1.29419664e-02,   4.95670268e-03,   7.83098325e-03,\n",
       "         1.03131864e-02,   2.51127894e-03,   6.96658178e-03,\n",
       "         5.63180993e-03,   4.03374679e-03,   1.61725925e-02,\n",
       "        -4.72025729e-04,   4.90050866e-03,   4.93890698e-03,\n",
       "         8.69902295e-03,   1.25942554e-02,   2.77999093e-03,\n",
       "         2.97695839e-03,   7.86085945e-03,   6.84940309e-03,\n",
       "         4.81790303e-03,   4.77458243e-03,   8.98942019e-03,\n",
       "         1.15612262e-02,   1.57886841e-02,   8.28692081e-03,\n",
       "         1.25774817e-02,   3.94118995e-03,   2.92259357e-03,\n",
       "         3.86209669e-03,   1.04958249e-02,   5.91439786e-03,\n",
       "         2.23190342e-02,   1.73796352e-02,   2.95637677e-03,\n",
       "         1.02041999e-02])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_items_scores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据用户对item的预测打分产生top推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Sort the indices of user_item_scores based upon their value\n",
    "#Also maintain the corresponding score\n",
    "sort_index = sorted(((e,i) for i,e in enumerate(list(user_items_scores))), reverse=True)\n",
    "    \n",
    "#Create a dataframe from the following\n",
    "columns = ['user_id', 'item', 'score', 'rank']\n",
    "df = pd.DataFrame(columns=columns)\n",
    "         \n",
    "#Fill the dataframe with top 20 item based recommendations\n",
    "rank = 1 \n",
    "for i in range(0,len(sort_index)):\n",
    "    cur_item_index = sort_index[i][1] \n",
    "    cur_item = list (item_index.keys()) [list (item_index.values()).index (cur_item_index)]\n",
    "            \n",
    "    if ~np.isnan(sort_index[i][0]) and cur_item_index not in cur_user_items and rank <= 20:\n",
    "        df.loc[len(df)]=[cur_user,cur_item,sort_index[i][0],rank]\n",
    "        rank = rank+1       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOZPMJT12AAF3B40D1</td>\n",
       "      <td>0.067380</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOACBLB12AB01871C7</td>\n",
       "      <td>0.058653</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOJSXJY12A8C13E32E</td>\n",
       "      <td>0.056914</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOGBOQX12A8C139DF7</td>\n",
       "      <td>0.050727</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SONXUQR12AB0186C9F</td>\n",
       "      <td>0.048499</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOVEDOZ12AB01873A1</td>\n",
       "      <td>0.042573</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUWZPO12A6D4F83E3</td>\n",
       "      <td>0.037406</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOELRWA12A8C142D0A</td>\n",
       "      <td>0.033408</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOVMADB12A8C137B96</td>\n",
       "      <td>0.032400</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOANOQW12A58A793D2</td>\n",
       "      <td>0.031346</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOSGEGU12AB018188A</td>\n",
       "      <td>0.030402</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOXQIUR12A8AE4654A</td>\n",
       "      <td>0.029769</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOCBSZW12AB01891C1</td>\n",
       "      <td>0.029115</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAJFTU12A6701E0A3</td>\n",
       "      <td>0.028858</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAOSDF12A58A779F1</td>\n",
       "      <td>0.028742</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOGSDHY12AB017BF39</td>\n",
       "      <td>0.028135</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOVAGPG12AB0189963</td>\n",
       "      <td>0.026204</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOWEFXC12A6D4FA230</td>\n",
       "      <td>0.025519</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOZCBWK12A58A7DFA5</td>\n",
       "      <td>0.025482</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAKPMX12AB018193B</td>\n",
       "      <td>0.025426</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     user_id                item     score  \\\n",
       "0   5a905f000fc1ff3df7ca807d57edb608863db05d  SOZPMJT12AAF3B40D1  0.067380   \n",
       "1   5a905f000fc1ff3df7ca807d57edb608863db05d  SOACBLB12AB01871C7  0.058653   \n",
       "2   5a905f000fc1ff3df7ca807d57edb608863db05d  SOJSXJY12A8C13E32E  0.056914   \n",
       "3   5a905f000fc1ff3df7ca807d57edb608863db05d  SOGBOQX12A8C139DF7  0.050727   \n",
       "4   5a905f000fc1ff3df7ca807d57edb608863db05d  SONXUQR12AB0186C9F  0.048499   \n",
       "5   5a905f000fc1ff3df7ca807d57edb608863db05d  SOVEDOZ12AB01873A1  0.042573   \n",
       "6   5a905f000fc1ff3df7ca807d57edb608863db05d  SOUWZPO12A6D4F83E3  0.037406   \n",
       "7   5a905f000fc1ff3df7ca807d57edb608863db05d  SOELRWA12A8C142D0A  0.033408   \n",
       "8   5a905f000fc1ff3df7ca807d57edb608863db05d  SOVMADB12A8C137B96  0.032400   \n",
       "9   5a905f000fc1ff3df7ca807d57edb608863db05d  SOANOQW12A58A793D2  0.031346   \n",
       "10  5a905f000fc1ff3df7ca807d57edb608863db05d  SOSGEGU12AB018188A  0.030402   \n",
       "11  5a905f000fc1ff3df7ca807d57edb608863db05d  SOXQIUR12A8AE4654A  0.029769   \n",
       "12  5a905f000fc1ff3df7ca807d57edb608863db05d  SOCBSZW12AB01891C1  0.029115   \n",
       "13  5a905f000fc1ff3df7ca807d57edb608863db05d  SOAJFTU12A6701E0A3  0.028858   \n",
       "14  5a905f000fc1ff3df7ca807d57edb608863db05d  SOAOSDF12A58A779F1  0.028742   \n",
       "15  5a905f000fc1ff3df7ca807d57edb608863db05d  SOGSDHY12AB017BF39  0.028135   \n",
       "16  5a905f000fc1ff3df7ca807d57edb608863db05d  SOVAGPG12AB0189963  0.026204   \n",
       "17  5a905f000fc1ff3df7ca807d57edb608863db05d  SOWEFXC12A6D4FA230  0.025519   \n",
       "18  5a905f000fc1ff3df7ca807d57edb608863db05d  SOZCBWK12A58A7DFA5  0.025482   \n",
       "19  5a905f000fc1ff3df7ca807d57edb608863db05d  SOAKPMX12AB018193B  0.025426   \n",
       "\n",
       "   rank  \n",
       "0     1  \n",
       "1     2  \n",
       "2     3  \n",
       "3     4  \n",
       "4     5  \n",
       "5     6  \n",
       "6     7  \n",
       "7     8  \n",
       "8     9  \n",
       "9    10  \n",
       "10   11  \n",
       "11   12  \n",
       "12   13  \n",
       "13   14  \n",
       "14   15  \n",
       "15   16  \n",
       "16   17  \n",
       "17   18  \n",
       "18   19  \n",
       "19   20  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
